<?php
include('../../Redirect_modules.php');
if(clean_param($_REQUEST['values'],PARAM_NOTAGS) && ($_POST['values'] || $_REQUEST['ajax']))
{
    foreach($_REQUEST['values'] as $id=>$columns)
    {
        if($id!='new')
        {
            $sql = 'UPDATE students_status SET ';
            foreach($columns as $column=>$value)
            {
                $value=trim(paramlib_validation($column,$value));
                if($column==NEXT_GRADE_ID && (str_replace("\'","''",$value)=='' || str_replace("\'","''",$value)==0 ))
                    $sql .= $column.'=NULL,';
                else
                    $sql .= $column.'=\''.str_replace("'","''",str_replace("\'","''",$value)).'\',';
            }
            $sql = substr($sql,0,-1) . ' WHERE ID=\''.$id.'\'';
            DBQuery($sql);
        }
        else
        {
            $sql='SELECT ID, SHORT_NAME, TITLE,SORT_ORDER FROM students_status';
            $studentstatuss=  DBGet(DBQuery($sql));
            for($i=1;$i<=count($studentstatuss);$i++)
            {
				$shortname[$i]=$studentstatuss[$i]['SHORT_NAME'];
                $status_title[$i]=$studentstatuss[$i]['TITLE'];
                $sort_order[$i]=$studentstatuss[$i]['SORT_ORDER'];
            }
            if(in_array($columns['TITLE'], $status_title))
            {
                $err_msg="Title already exists";
                break;
            }
            else
            {

                {
                    if(in_array($columns['SORT_ORDER'], $shortname))
                    {
                        $err_msg="Sort order already exists";
                        break;
                    }
                    else
                    {
                        if(clean_param(trim($_REQUEST['values']['new']['TITLE']),PARAM_NOTAGS)!='')
                        {
                            $sql = 'INSERT INTO students_status ';
                            //$fields = 'SCHOOL_ID,';
                            $fields = '';
                            //$values = '\''.UserSchool().'\',';
                            $values='';
                            $go = 0;
                            foreach($columns as $column=>$value)
                            {
                                if(trim($value))
                                {
                                    $value=trim(paramlib_validation($column,$value));
                                    $fields .= $column.',';
                                    $values .= '\''.str_replace("'","''",str_replace("\'","''",$value)).'\',';
                                    $go = true;
                                }
                            }
                            $sql .= '(' . substr($fields,0,-1) . ') values(' . substr($values,0,-1) . ')';

                            if($go)
                                DBQuery($sql);
                        }
                    }
                }
            }
        }
    }
}
DrawBC("School Setup > ".ProgramTitle());
#echo "Modules.php?modname=$_REQUEST[modname]";
if(clean_param($_REQUEST['modfunc'],PARAM_ALPHAMOD)=='remove')
{
    //$grd_id=paramlib_validation($colmn=PERIOD_ID,$_REQUEST[id]);
    $has_assigned_RET=DBGet(DBQuery('SELECT COUNT(*) AS TOTAL_ASSIGNED FROM students WHERE status_id='.$_REQUEST[id]));
    $has_assigned=$has_assigned_RET[1]['TOTAL_ASSIGNED'];
    if($has_assigned>0){
        UnableDeletePrompt('Cannot delete because class are associated.');
    }else{
        if(DeletePrompt_GradeLevel('grade level'))
        {
            DBQuery("DELETE FROM students_status WHERE ID='$_REQUEST[id]'");
            unset($_REQUEST['modfunc']);
        }
    }
}

if($_REQUEST['modfunc']!='remove')
{
    $sql = 'SELECT ID, SHORT_NAME, TITLE,SORT_ORDER FROM students_status ORDER BY SORT_ORDER';
    $QI = DBQuery($sql);
    $status_RET = DBGet($QI,array('TITLE'=>'makeTextInput','SHORT_NAME'=>'makeTextInput','SORT_ORDER'=>'makeTextInput'));

    $columns = array('TITLE'=>'Title','SHORT_NAME'=>'Short Name','SORT_ORDER'=>'Sort Order');
    $link['add']['html'] = array('TITLE'=>makeTextInput('','TITLE'),'SHORT_NAME'=>makeTextInput('','SHORT_NAME'),'SORT_ORDER'=>makeTextInputMod2('','SORT_ORDER'));
    $link['remove']['link'] = "Modules.php?modname=$_REQUEST[modname]&modfunc=remove";
    $link['remove']['variables'] = array('id'=>'ID');
    if($err_msg)
    {
        echo "<b style='color:red'>".$err_msg."</b>";

        unset($err_msg);
    }
    echo "<FORM name=F1 id=F1 action=Modules.php?modname=$_REQUEST[modname]&modfunc=update method=POST>";
    #DrawHeader('','<INPUT type=submit value=Save>');
    ListOutput($status_RET,$columns,'Status','Status',$link, true, array('search'=>false));
    echo '<br><CENTER><INPUT class="btn_medium" type=submit value=Save onclick="formcheck_school_setup_grade_levels();"></CENTER>';
    echo '</FORM>';
}

function makeTextInput($value,$name)
{	global $THIS_RET;

    if($THIS_RET['ID'])
        $id = $THIS_RET['ID'];
    else
        $id = 'new';

    if($name!='TITLE')
        $extra = 'size=5 maxlength=5 class=cell_floating';
    else
        $extra = 'class=cell_floating ';

    if($name=='SORT_ORDER')
    {
        if($id=='new')
            $extra = 'size=5 maxlength=5 class=cell_floating onKeyDown="return numberOnly(event);"';
        else
            $extra = 'size=5 maxlength=5 class=cell_floating onKeyDown=\"return numberOnly(event);\"';

        $comment = '<!-- '.$value.' -->';
    }

    return $comment.TextInput($value,'values['.$id.']['.$name.']','',$extra);
}

function makeTextInputMod1($value,$name)
{	global $THIS_RET;
    if($THIS_RET['ID'])
        $id = $THIS_RET['ID'];
    else
        $id = 'new';

    if($name!='TITLE')
        $extra = 'size=5 maxlength=2 class=cell_floating';
    else
        $extra = 'class=cell_floating ';
    if($name=='SORT_ORDER')
        $comment = '<!-- '.$value.' -->';

    return $comment.TextInput($value,'values['.$id.']['.$name.']','',$extra);
}

function makeTextInputMod2($value,$name)
{	global $THIS_RET;
    if($THIS_RET['ID'])
        $id = $THIS_RET['ID'];
    else
        $id = 'new';

    if($name!='TITLE')
        $extra = 'size=5 maxlength=5 class=cell_floating ';
    else
        $extra = 'class=cell_floating ';
    if($name=='SORT_ORDER')
    {
        if($id=='new')
            $extra = 'size=5 maxlength=5 class=cell_floating onKeyDown="return numberOnly(event);"';
        else
            $extra = 'size=5 maxlength=5 class=cell_floating onKeyDown=\"return numberOnly(event);\"';

        $comment = '<!-- '.$value.' -->';
    }
    return $comment.TextInput($value,'values['.$id.']['.$name.']','',$extra);
}


function makeGradeInput($value,$name)
{	global $THIS_RET,$statuss;

    if($THIS_RET['ID'])
        $id = $THIS_RET['ID'];
    else
        $id = 'new';

    if(!$statuss)
    {
        $status_RET = DBGet(DBQuery('SELECT ID, TITLE FROM students_status WHERE SCHOOL_ID=\''.UserSchool().'\' ORDER BY SORT_ORDER'));
        if(count($status_RET))
        {
            foreach($status_RET as $status)
                $statuss[$status['ID']] = $status['TITLE'];
        }
    }

    return SelectInput($value,'values['.$id.']['.$name.']','',$statuss,'N/A');
}



?>